package com.phiture.erp.purchase.core.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.phiture.erp.purchase.core.pojo.dto.CreatePurchaseOrderMergeEntryDTO;
import com.phiture.erp.purchase.core.service.ErpPurchaseOrderMergeEntryService;
import com.phiture.erp.purchase.dal.entity.ErpPurchaseOrderMergeEntryDO;
import com.phiture.erp.purchase.dal.mapper.ErpPurchaseOrderMergeEntryMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YH
 * @date 2025-05-30-14:18
 */

@Service
@RequiredArgsConstructor
public class ErpPurchaseOrderMergeEntryServiceImpl implements ErpPurchaseOrderMergeEntryService {
    private final ErpPurchaseOrderMergeEntryMapper erpPurchaseOrderMergeEntryMapper;

    @Override
    public void createPurchaseOrderMergeEntry(String purchaseOrderNo,
                                              Long purchaseRequestId,
                                              Long purchaseRequestItemId,
                                              Long materialId,
                                              BigDecimal qty) {
        ErpPurchaseOrderMergeEntryDO entryDO = new ErpPurchaseOrderMergeEntryDO();
        entryDO.setPurchaseOrderNo(purchaseOrderNo).setPurchaseRequestId(purchaseRequestId)
                .setPurchaseRequestItemId(purchaseRequestItemId).setMaterialId(materialId).setQty(qty);
        erpPurchaseOrderMergeEntryMapper.insert(entryDO);
    }

    @Override
    public void batchCreatePurchaseOrderMergeEntry(Collection<CreatePurchaseOrderMergeEntryDTO> entrys) {
        if(CollUtil.isEmpty(entrys)) return;
        List<ErpPurchaseOrderMergeEntryDO> list = entrys.stream().map(item -> {
            ErpPurchaseOrderMergeEntryDO entryDO = new ErpPurchaseOrderMergeEntryDO();
            entryDO.setPurchaseOrderNo(item.getPurchaseOrderNo()).setPurchaseRequestId(item.getPurchaseRequestId())
                    .setPurchaseRequestItemId(item.getPurchaseRequestItemId()).setMaterialId(item.getMaterialId()).setQty(item.getQty());
            return entryDO;
        }).collect(Collectors.toList());
        erpPurchaseOrderMergeEntryMapper.insertBatch(list);
    }
}
